package com.nanyang.tms.controller.supplier;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.dto.request.supplier.SupplierAddRequest;
import com.nanyang.tms.api.dto.request.supplier.SupplierQueryRequest;
import com.nanyang.tms.api.dto.request.supplier.SupplierUpdateRequest;
import com.nanyang.tms.api.dto.request.supplier.SupplierUpdateStateRequest;
import com.nanyang.tms.api.dto.response.supplier.SupplierPageKeyResponse;
import com.nanyang.tms.api.dto.response.supplier.SupplierPageResponse;
import com.nanyang.tms.api.dto.response.supplier.SupplierResponse;
import com.nanyang.tms.api.service.supplier.SupplierRpcService;

@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private SupplierRpcService supplierRpcService;

    /**
     * <p>listSupplier</p>
     * <p>Description:查询服务商列表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<SupplierPageResponse>> listSupplier(@RequestBody SupplierQueryRequest param) {
        ApiResult<PageBean<SupplierPageResponse>> apiResult = supplierRpcService.listSupplier(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listSupplier</p>
     * <p>Description:模糊查询服务商列表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/listByKey", method = RequestMethod.POST)
    public ApiResult<PageBean<SupplierPageKeyResponse>> listSupplierByKey(@RequestBody SupplierQueryRequest param) {
        ApiResult<PageBean<SupplierPageKeyResponse>> apiResult = supplierRpcService.listSupplierByKey(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listSupplier</p>
     * <p>Description:查询根据编码</p>
     *
     * @param supplierCode
     * @return
     */
    @RequestMapping(value = "/selectByCode/{supplierCode}", method = RequestMethod.GET)
    public ApiResult<SupplierResponse> selectByCode(@PathVariable("supplierCode") String supplierCode) {
        ApiResult<SupplierResponse> apiResult = supplierRpcService.selectByCode(supplierCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 添加
     * @param param
     * @return
     */

    @BindParam
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ApiResult<Boolean> saveSupplier( @BindParam @RequestBody @Valid SupplierAddRequest param) {
        ApiResult<Boolean> apiResult = supplierRpcService.saveSupplier(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 修改
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResult<Boolean> updateSupplier(  @BindParam @RequestBody @Valid SupplierUpdateRequest param) {
        ApiResult<Boolean> apiResult = supplierRpcService.updateSupplier(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 删除
     * @param supplierCode
     * @return
     */
    @BindParam
    @RequestMapping(value = "/del/{supplierCode}", method = RequestMethod.GET)
    public ApiResult<Boolean> delSupplier(@PathVariable("supplierCode") String supplierCode, @Value("") @BindParam String pin) {
        ApiResult<Boolean> apiResult = supplierRpcService.delSupplier(supplierCode,pin);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 禁用
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    public ApiResult<Boolean> disableState(@BindParam @RequestBody @Valid SupplierUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = supplierRpcService.disableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 启用
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public ApiResult<Boolean> enableState(@BindParam @RequestBody @Valid SupplierUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = supplierRpcService.enableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
